package com.zzyy.study.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.openai.OpenAiAudioSpeechClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.io.FileUtil;

import java.io.File;
/**
 * @auther zzyy
 * @create 2024-05-19 12:53
 */
@RestController
public class TTsController
{
    @Resource
    private OpenAiAudioSpeechClient openAiAudioSpeechClient;

    /**
     * 文本转语音
     * @return
     */
    @GetMapping(value = "/ai/tts")
    public String tts()
    {
        String textContent = "先帝创业未半而中道崩殂，今天下三分，益州疲弊，此诚危急存亡之秋也。" +
                "然侍卫之臣不懈于内，忠志之士忘身于外者，盖追先帝之殊遇，欲报之于陛下也。";

        byte[] mp3Data = openAiAudioSpeechClient.call(textContent);
        String filePath = "D:\\44\\出师表Test.mp3";

        try
        {
            // 使用hutool的FileUtil来写入文件
            FileUtil.writeBytes(mp3Data,new File(filePath));
            System.out.println("MP3文件已成功写入到硬盘！");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("写入MP3文件时发生错误！");
        }

        return "MP3文件已成功写入到硬盘！";
    }
}
